﻿package pages
{
	import com.gaiaframework.templates.AbstractPage;
	import com.gaiaframework.events.*;
	import com.gaiaframework.debug.*;
	import com.gaiaframework.api.*;
	import com.greensock.plugins.*;
	import flash.display.*;
	import flash.events.*;
	import com.greensock.TweenMax;
	import flash.utils.Dictionary;
	import model.Model;
	import ui.Explanation;
	import ui.footer.Footer;
	import ui.footer.FooterMenu;
	import ui.Scroller;
	import utils.ShapeUtil;
	import utils.TextFieldUtil;
	
	public class NavPage extends AbstractPage
	{	
		public var footer:Footer;
		private var _calage:Shape;
		private var _logo:Logo;
		private var _isOpen:Boolean = false;
		private var _footerMenu:FooterMenu;
		public var _explanationMc:Explanation;
		public function NavPage()
		{
			super();
			alpha = 0;
			//new Scaffold(this);
		}
		override public function transitionIn():void 
		{
			super.transitionIn();
			TweenPlugin.activate([ShortRotationPlugin, DropShadowFilterPlugin, TransformAroundPointPlugin]);
			TextFieldUtil.setStyle(IStyleSheet(Gaia.api.getPage(Pages.INDEX).assets.styles).style);
			Model.instance.uiData = IXml(Gaia.api.getPage(Pages.INDEX).assets.uiData).xml;
			Model.instance.traceAvailableFonts();
			_addPresentation();
			_addFooter();
			addMenu();
			_addLogo();
			_onResize();
			stage.addEventListener(Event.RESIZE, _onResize);
			TweenMax.to(this, 0.3, { alpha:1, onComplete:transitionInComplete } );
		}
		
		private function _addPresentation():void 
		{
			_explanationMc = new Explanation();
			addChild(_explanationMc);
			_explanationMc.visible = false;
		}
		
		private function addMenu():void 
		{
			_footerMenu = new FooterMenu();
			addChild(_footerMenu)
		}
		
		public function updateNav():void
		{
			_isOpen = !_isOpen;
			
			if (_isOpen)
			{
				footer.open();
				TweenMax.delayedCall(.5, function():void { Gaia.api.getPage(Pages.TIMELINE).content.updateTimeLine('over') } );
				//footer.addEventListener(MouseEvent.MOUSE_OVER, overFooter);
				//footer.addEventListener(MouseEvent.MOUSE_OUT, outFooter);
			}else
			{
				footer.close();
				//footer.removeEventListener(MouseEvent.MOUSE_OVER, overFooter);
				//footer.removeEventListener(MouseEvent.MOUSE_OUT, outFooter);
			}
			trace("[NavPage.updateNav] _isOpen=",_isOpen);
		}
		
		private function outFooter(e:MouseEvent):void 
		{
			trace("[NavPage.outFooter] ");
			footer.close();
		}
		
		private function overFooter(e:MouseEvent):void 
		{
			trace("[NavPage.overFooter] ");
			footer.open();
		}
		
		private function _addFooter():void 
		{
			footer = new Footer();
			addChild(footer)
		}
		
		private function _addLogo():void 
		{
			_logo = new Logo();
			_logo.buttonMode = true;
			_logo.addEventListener(MouseEvent.CLICK, function():void { Gaia.api.goto(Pages.HOME); _explanationMc.visible = false; } );
			addChild(_logo)
		}
		

		public function _onResize(e:Event=null):void 
		{
			var sh:Number = stage.stageHeight;
			var sw:Number = stage.stageWidth;
			if (footer)
			{
				footer.resize();
				//footer.y = sh - footer.height;
			}
			if (_footerMenu)
			{
				_footerMenu.x =  int(sw - _footerMenu.width - 20);
				_footerMenu.y = int(sh - 25);
			}
			if (_logo)
			{
				_logo.x = sw - _logo.width;
				_logo.y = 17;
			}
		}
		override public function transitionOut():void 
		{
			super.transitionOut();
			TweenMax.to(this, 0.3, {alpha:0, onComplete:transitionOutComplete});
		}
	}
}
